<template>
  <view class="goods-detail">
    <view class="goods-detail-img">
      <!-- 商品详情轮播图 -->
      <view class="goods-wrap">
        <u-swiper @click="handlePreviewImag"
                  :list="detailData.pics"
                  bg-color="#fff"
                  name="pics_big"
                  height="497"
                  img-mode="aspectFit"></u-swiper>
      </view>
      <!-- 商品价格部分 -->
      <view class="goods-contnet">
        <view class="goods-contnet-top">￥<text>{{detailData.goods_price}}</text></view>
        <view class="goods-contnet-bottom">{{detailData.goods_name}}</view>
      </view>
    </view>
    <!-- 图文详情 -->
    <view class="TXT-detail">
      <view class="TXT-detail-title">图文详情</view>
      <view class="rich-text">
        <!-- 如果是简单的场景，比如一段简单的文字和图片内容，可以优先使用rich-text组件，在文章内容，商品详情等复杂的文本详情，可以优先使用parse组件。 -->
        <u-parse :html="detailData.goods_introduce"></u-parse>
        <!-- <rich-text :nodes="detailData.goods_introduce"></rich-text> -->
      </view>
    </view>
    <!-- 底部导航 -->
    <!-- <view class="goods-detail-nav">
        <view class="goods-detail-bottom icon service"><u-icon name="kefu-ermai" size="40"></u-icon>联系客服</view>
        <view class="goods-detail-bottom icon cart"><u-icon name="shopping-cart" size="40"></u-icon>购物车</view>
        <view class="goods-detail-bottom add-cart">加入购物车</view>
        <view class="goods-detail-bottom buy-now">立即购买</view>
    </view> -->
    <view class="navigation">
      <view class="left">
        <view class="item">
          <u-icon name="server-fill"
                  :size="40"
                  :color="$u.color['contentColor']"></u-icon>
          <view class="text u-line-1">客服</view>
        </view>
        <view class="item">
          <u-icon name="home"
                  :size="40"
                  :color="$u.color['contentColor']"></u-icon>
          <view class="text u-line-1">店铺</view>
        </view>
        <view class="item car">
          <navigator open-type="switchTab"
                     url="/pages/cart/cart">
            <u-badge class="car-num"
                     :count="nums"
                     type="error"
                     :offset="[-3, -6]"></u-badge>
            <u-icon name="shopping-cart"
                    :size="40"
                    :color="$u.color['contentColor']"></u-icon>
            <view class="text u-line-1">购物车</view>
          </navigator>
        </view>
      </view>
      <view class="right">
        <view class="cart btn u-line-1"
              @click="addCart">加入购物车</view>
        <view class="buy btn u-line-1">立即购买</view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      detailData: null,
    }
  },
  onLoad({ goods_id }) {
    this.getGoodsDetailById(goods_id)
    // console.log(this.cartList)
  },
  computed: {
    ...mapState({
      nums: (state) =>
        state.cart.cartList.reduce((count, item) => (count += item.nums), 0),
    }),
  },
  methods: {
    //   获取商品详情
    async getGoodsDetailById(goods_id) {
      this.detailData = await this.$u.get('/goods/detail', { goods_id })
    },
    //   点击图片放大预览
    handlePreviewImag(current) {
      const urls = this.detailData.pics.map((url) => url.pics_big)
      uni.previewImage({ urls, current })
    },
    // ⭐可以通过⭐的方法调用vuex模块中的mutations里里面的方法
    // ⭐获取vuex mutatations
    // ⭐...mapMutations(["cart/cartAdd"]),
    //  点击加入购物车
    addCart() {
      // ⭐this["cart/cartAdd"]({ value: this.goods });
      this.$store.commit('cart/addCart', this.detailData)
    },
  },
}
</script>

<style lang="scss" scoped>
.goods-detail {
  padding-bottom: 102rpx;
  background-color: #f9f9f9;
  .goods-detail-img {
    height: 717rpx;
    background-color: #fff;
    border-bottom-left-radius: 20rpx;
    border-bottom-right-radius: 20rpx;
    .goods-contnet {
      padding: 0 34rpx;
      .goods-contnet-top {
        line-height: 58rpx;
        color: #ea4350;
        font-weight: 700;
        margin: 19rpx 0;
        text {
          font-size: 36rpx;
        }
      }
      .goods-contnet-bottom {
        // height: 84px;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        font-size: 30rpx;
        font-weight: 700;
        -webkit-box-orient: vertical;
      }
    }
  }
  .TXT-detail {
    margin-top: 20rpx;
    background-color: #fff;
    .TXT-detail-title {
      font-size: 30rpx;
      padding: 19rpx 0;
      font-weight: 700;
      text-align: center;
    }
  }
  .goods-detail-nav {
    display: flex;
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 94rpx;
    align-items: center;
    padding: 0 15rpx;
    background-color: #fff;
    .goods-detail-bottom {
      text-align: center;
      margin: 0 15rpx;
    }
    .icon {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .buy-now,
    .add-cart {
      color: #fff;
      width: 196rpx;
      line-height: 60rpx;
      border-radius: 50rpx;
    }
    .add-cart {
      background-color: #fcaa23;
    }
    .buy-now {
      background-color: #ea4350;
    }
  }
}
.navigation {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  margin-top: 100rpx;
  border: solid 2rpx #f2f2f2;
  background-color: #ffffff;
  padding: 16rpx 0;
  .left {
    display: flex;
    font-size: 20rpx;
    .item {
      margin: 0 30rpx;
      &.car {
        text-align: center;
        position: relative;
        .car-num {
          position: absolute;
          top: -10rpx;
          right: -10rpx;
        }
      }
    }
  }
  .right {
    display: flex;
    font-size: 28rpx;
    align-items: center;
    .btn {
      line-height: 66rpx;
      padding: 0 30rpx;
      border-radius: 36rpx;
      color: #ffffff;
    }
    .cart {
      background-color: #ed3f14;
      margin-right: 30rpx;
    }
    .buy {
      background-color: #ff7900;
    }
  }
}
</style>